//
//  CustomLoadingDataView.m
//  SafePeriod
//
//  Created by Sheng long on 13-6-5.
//  Copyright (c) 2013年 cdeledu. All rights reserved.
//

#import "AutoLoadingMoreView.h"
#import "AutoTableConstant.h"

@interface AutoLoadingMoreView()
{
    //提示Label
    UILabel *tipLabel;
    
    //显示正在加载
    NSTimer *timer;
    
    //正在刷新加载框
	UIActivityIndicatorView *activityView;
}

@end

@implementation AutoLoadingMoreView

@synthesize currentDataState,delegate;

-(void)dealloc
{
    if (tipLabel) {
        [tipLabel release];
        tipLabel = nil;
    }
    
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    
    if (activityView) {
        [activityView release];
        activityView = nil;
    }
    
    [super dealloc];
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        tipLabel = [[UILabel alloc]initWithFrame:self.bounds];
        [tipLabel setBackgroundColor:[UIColor clearColor]];
        [tipLabel setTextAlignment:NSTextAlignmentCenter];
        [tipLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
        [tipLabel setTextColor:TABEL_VISIBLE_REFRESH_STATE_COLOR];
        [self addSubview:tipLabel];
        
        activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self addSubview:activityView];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:self.bounds];
        [self addSubview:button];
        [button addTarget:self action:@selector(clickButt) forControlEvents:UIControlEventTouchUpInside];
        
        //间隔线
        UIView *gapView = [[UIView alloc]initWithFrame:CGRectMake(0.0,  frame.size.height-1, frame.size.width,0.5)];
        [gapView setBackgroundColor:[UIColor lightTextColor]];
        [self addSubview:gapView];
        [gapView release];
        UIView *gapView1 = [[UIView alloc]initWithFrame:CGRectMake(0.0,  frame.size.height-0.5, frame.size.width,0.5)];
        [gapView1 setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:gapView1];
        [gapView1 release];
        
        //默认设置加载更多状态
        [self setCurrentDataState:GetMoreDataState];
    }
    return self;
}

//获取更多方法
-(void)clickButt
{
    //当前状态是获取更多，执行点击方法
    if (currentDataState == GetMoreDataState || currentDataState == GetMoreDataExceptionState) {
        if ([(NSObject*)self.delegate respondsToSelector:@selector(loadingMoreData)]) {
            [self setCurrentDataState:LoadingDataState];
            [self.delegate loadingMoreData];
        }
    }
}

//设置刷新获取数据view
-(void)setCurrentDataState:(CurrentDataState)state
{
    currentDataState = state;

    BOOL isShow = NO;
    NSString *tipStr = nil;
    switch (currentDataState) {
        case TemporarilyNoDataState:
        {
            //暂无数据
            tipStr = @"暂无可用信息";
        }
            break;
        case LoadingDataState:
        {
            //正在加载
            isShow = YES;
            tipStr = @"正在加载更多...";
        }
            break;
        case GetMoreDataState:
        {
            //加载更多
            tipStr = @"点击加载更多";
        }
            break;
        case GetMoreDataExceptionState:
        {
            //加载更多
            tipStr = @"加载失败，点击重试";
        }
            break;
        case AlreadyLoadedState:
        {
            //已加载全部
            tipStr = @"暂无更多信息";
        }
            break;
            
        default:
            break;
    }    
    [tipLabel setText:tipStr];
    
    [tipLabel sizeToFit];
    if (isShow) {
        //间隔
        CGFloat gap = 5;
        [activityView startAnimating];
        activityView.center = CGPointMake((self.frame.size.width-activityView.frame.size.width-gap-tipLabel.frame.size.width)/2+activityView.frame.size.width/2, self.frame.size.height/2);
        [tipLabel setCenter:CGPointMake(activityView.frame.size.width+activityView.frame.origin.x+gap+tipLabel.frame.size.width/2, self.frame.size.height/2)];
    }else{
        [activityView stopAnimating];
        [tipLabel setCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];
    }
    
//    if (isShow) {
//        //定时器刷新UI
//        timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
//        //防止UI卡定时器
//        NSRunLoop *main=[NSRunLoop currentRunLoop];
//        [main addTimer:timer forMode:NSRunLoopCommonModes];
//        
//    }else{
//        [timer invalidate];
//        timer = nil;
//    }
}

//定时器刷新UI
-(void)updateProgress:(id)sender
{
    NSString *text = tipLabel.text;
    if (text.length<10) {        
        text = [text stringByAppendingString:@"."];
    }else{
        text = @"正在加载";
    }
    [tipLabel setText:text];
}

@end
